﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace 云起停车_岗亭端
{
    public partial class PlateNumber : Form
    {
        private Barrier barrier = null;

        private string[] platenumber = new string[] { "贵", "", "", "", "", "", "", "" };
        private string[] platecolor = new string[] { "蓝牌", "黄牌", "绿牌", "黄绿", "白牌", "黑牌" };
        private string[] platecolorlist = new string[] { "blue", "yellow", "green", "yellow-green", "white", "black" };
        private int colorIndex = 0;
        private string[] province = new string[] { "京", "津", "渝", "沪", "冀", "晋", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "琼", "川", "贵", "云", "陕", "甘", "青", "蒙", "桂", "宁", "新", "藏", "临" };
        private string[] font = new string[] { "使", "领", "警", "学", "港", "澳" };
        private string[] number = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        private int activeIndex = 1;


        private List<Button> plateNumberButtons;
        private List<Button> provinceButtons;
        private List<Button> numberButtons;
        private List<Button> fontButtons;
        private List<Button> colorButtons;

        public PlateNumber(Barrier barrier)
        {
            InitializeComponent();
            this.barrier = barrier;
            InitializeUI();
        }

        private void InitializeUI()
        {
            //隐藏窗体顶部的放大、缩小按钮
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            // 标题
            Label titleLabel = new Label();
            titleLabel.Text = "请点击输入车牌号";
            titleLabel.Font = new Font("Microsoft YaHei", 22);
            titleLabel.TextAlign = ContentAlignment.MiddleCenter;
            titleLabel.Dock = DockStyle.Top;
            titleLabel.Width = this.Width;
            titleLabel.Height = 60;

            // 车牌号输入框
            FlowLayoutPanel plateInputPanel = new FlowLayoutPanel();
            plateInputPanel.Dock = DockStyle.Top;
            plateInputPanel.Padding = new Padding(125, 10, 10, 10);
            plateInputPanel.BackColor = Color.DeepSkyBlue;
            plateInputPanel.FlowDirection = FlowDirection.LeftToRight;
            plateInputPanel.Width = this.Width;
            plateInputPanel.Height = 80;
            plateInputPanel.Margin = new Padding(0);

            plateNumberButtons = new List<Button>();
            for (int i = 0; i < 8; i++)
            {
                Button button = new Button();
                button.Text = platenumber[i];
                button.Width = 50; // 调整宽度
                button.Height = 50; // 调整高度
                button.Font = new Font("宋体", 20); // 调整字体大小
                button.TextAlign = ContentAlignment.MiddleCenter; // 文本居中
                button.Click += (sender, e) => PlateNumberButton_Click(Array.IndexOf(plateNumberButtons.ToArray(), sender));
                plateInputPanel.Controls.Add(button);
                plateNumberButtons.Add(button);

                if (i == 1)
                {
                    Label dotLabel = new Label();
                    dotLabel.Text = "·";
                    dotLabel.Font = new Font("宋体", 33);
                    dotLabel.Width = 50; // 调整宽度
                    dotLabel.Height = 50; // 调整高度
                    dotLabel.TextAlign = ContentAlignment.MiddleCenter; // 文本居中
                    plateInputPanel.Controls.Add(dotLabel);
                }
            }

            // 省份简称
            FlowLayoutPanel provincePanel = new FlowLayoutPanel();
            provincePanel.Dock = DockStyle.Top;
            provincePanel.Padding = new Padding(10, 10, 10, 10);
            provinceButtons = new List<Button>();
            foreach (string item in province)
            {
                Button button = new Button();
                button.Text = item;
                button.Width = 38;
                button.Height = 38;
                button.Font = new Font("宋体", 18);
                button.Click += (sender, e) => ProvinceButton_Click(item);
                provincePanel.Controls.Add(button);
                provinceButtons.Add(button);
            }
            // 数字和字母
            FlowLayoutPanel numberPanel = new FlowLayoutPanel();
            numberPanel.Dock = DockStyle.Top;
            numberPanel.Padding = new Padding(10, 10, 10, 10);
            numberButtons = new List<Button>();
            foreach (string item in number)
            {
                Button button = new Button();
                button.Text = item;
                button.Width = 38;
                button.Height = 38;
                button.Font = new Font("宋体", 18);
                button.Click += (sender, e) => NumberButton_Click(item);
                numberPanel.Controls.Add(button);
                numberButtons.Add(button);
            }

            // 特殊字符和颜色选择
            FlowLayoutPanel fontPanel = new FlowLayoutPanel();
            fontPanel.Dock = DockStyle.Top;
            fontPanel.Padding = new Padding(10, 10, 10, 10);
            fontPanel.Height = 60;
            fontButtons = new List<Button>();
            foreach (string item in font)
            {
                Button button = new Button();
                button.Text = item;
                button.Width = 38;
                button.Height = 38;
                button.Font = new Font("宋体", 18);
                button.Click += (sender, e) => FontButton_Click(item);
                fontPanel.Controls.Add(button);
                fontButtons.Add(button);
            }
            colorButtons = new List<Button>();
            for (int i = 0; i < platecolor.Length; i++)
            {
                Button button = new Button();
                button.Text = platecolor[i];
                button.Width = 75;
                button.Height = 38;
                button.Font = new Font("宋体", 18);
                button.Click += (sender, e) => ColorButton_Click(Array.IndexOf(colorButtons.ToArray(), sender));
                fontPanel.Controls.Add(button);
                colorButtons.Add(button);
            }

            // 底部按钮容器
            FlowLayoutPanel buttonPanel = new FlowLayoutPanel
            {
                Dock = DockStyle.Top,
                Width = this.Width,
                Height = 60,
                //从右往左布局
                FlowDirection = FlowDirection.RightToLeft,
                Padding = new Padding(0, 0, 80, 0),
            };
 
            if (this.barrier.barrier_type.Equals("entry"))
            {
                // 确认按钮
                Button entryButton = new Button
                {
                    Text = "确认入场",
                    Width = 100,
                    Height = 40,
                    Margin = new Padding(0, 0, 10, 0),
                    //字体大小
                    Font = new Font("Microsoft YaHei", 12),
                    BackColor = Color.LightGreen
                };
                // 取消按钮
                Button cancelButton = new Button
                {
                    Text = "取消入场",
                    Width = 100,
                    Height = 40,
                    Margin = new Padding(0, 0, 0, 0),
                    Font = new Font("Microsoft YaHei", 12),
                    BackColor = Color.LightGray
                };
                entryButton.Click += clickEntry;
                cancelButton.Click += clickCancel;
                buttonPanel.Controls.Add(cancelButton);
                buttonPanel.Controls.Add(entryButton);
            }
            else if (this.barrier.barrier_type.Equals("exit"))
            {
                // 免费出场
                Button freeButton = new Button
                {
                    Text = "免费出场",
                    Width = 100,
                    Height = 40,
                    Margin = new Padding(0, 0, 10, 0),
                    //字体大小
                    Font = new Font("Microsoft YaHei", 12),
                    BackColor = Color.LightGreen
                };
                // 缴费出场
                Button payButton = new Button
                {
                    Text = "缴费出场",
                    Width = 100,
                    Height = 40,
                    Margin = new Padding(0, 0, 10, 0),
                    //字体大小
                    Font = new Font("Microsoft YaHei", 12),
                    BackColor = Color.YellowGreen
                };
                // 欠费出场
                Button arrearsButton = new Button
                {
                    Text = "欠费出场",
                    Width = 100,
                    Height = 40,
                    Margin = new Padding(0, 0, 10, 0),
                    //字体大小
                    Font = new Font("Microsoft YaHei", 12),
                    BackColor = Color.OrangeRed
                };
                // 取消按钮
                Button cancelButton = new Button
                {
                    Text = "取消出场",
                    Width = 100,
                    Height = 40,
                    Margin = new Padding(0, 0, 0, 0),
                    Font = new Font("Microsoft YaHei", 12),
                    BackColor = Color.LightGray
                };
                freeButton.Click += clickFree;
                payButton.Click += clickPay;
                arrearsButton.Click += clickArrears;
                cancelButton.Click += clickCancel;
                buttonPanel.Controls.Add(cancelButton);
                buttonPanel.Controls.Add(freeButton);
                buttonPanel.Controls.Add(payButton);
                buttonPanel.Controls.Add(arrearsButton);
            }
            this.Controls.Add(buttonPanel);
            this.Controls.Add(fontPanel);
            this.Controls.Add(numberPanel);
            this.Controls.Add(provincePanel);
            this.Controls.Add(plateInputPanel);
            this.Controls.Add(titleLabel);

            UpdateButtonStates();
        }

        private void clickCancel(object sender, EventArgs e)
        {
            this.Close();
        }

        private void clickEntry(object sender, EventArgs e)
        {
            this.confirm();
        }

        private void clickFree(object sender, EventArgs e)
        {
            this.confirm("x4");
        }


        private void clickPay(object sender, EventArgs e)
        {
            this.confirm("x3");
        }

        private void clickArrears(object sender, EventArgs e)
        {
            this.confirm("x7");
        }


        private async void confirm(string pay_status = null)
        {
            //判断车牌号是否完整
            if (
                platenumber[0] == "" 
                || platenumber[1] == "" 
                || platenumber[2] == "" 
                || platenumber[3] == "" 
                || platenumber[4] == "" 
                || platenumber[5] == "" 
                || platenumber[6] == ""
            )
            {
                MessageBox.Show("车牌号不完整，请重新输入");
                return;
            }
            //将platenumber转成字符串
            string plateNumberString = string.Join("", platenumber);
            JObject obj = new JObject();
            obj.Add("barrier_id", this.barrier.id);
            obj.Add("platenumber", plateNumberString);
            obj.Add("plate_type", this.platecolorlist[this.colorIndex]);
            if (this.barrier.barrier_type.Equals("entry"))
            {
                Response response = await Utils.PostAsync("sentrybox/index/entry", obj, this);
                if (null == response)
                {
                    return;
                }
                MessageBox.Show("入场成功");
                this.Close();
            }
            if (this.barrier.barrier_type.Equals("exit"))
            {
                obj.Add("pay_status", pay_status);
                Response response = await Utils.PostAsync("sentrybox/index/exit", obj, this);
                if (null == response)
                {
                    return;
                }
                MessageBox.Show("出场成功");
                this.Close();
            }
        }

        private void PlateNumberButton_Click(int index)
        {
            if (this.checkSpesialCharacter() && index==7) {
                return;
            }
            activeIndex = index;
            UpdateButtonStates();
        }

        private void ProvinceButton_Click(string item)
        {
            if (activeIndex == 0)
            {
                platenumber[activeIndex] = item;
                plateNumberButtons[activeIndex].Text = item;
                if (activeIndex < 7 && !this.checkSpesialCharacter())
                {
                    activeIndex++;
                }
                UpdateButtonStates();
            }
        }

        private void NumberButton_Click(string item)
        {
            if (activeIndex != 0)
            {
                platenumber[activeIndex] = item;
                plateNumberButtons[activeIndex].Text = item;
                if (activeIndex < 7 && !this.checkSpesialCharacter())
                {
                    activeIndex++;
                }
                UpdateButtonStates();
            }
        }

        private void FontButton_Click(string item)
        {
            if (activeIndex == 6 || activeIndex == 7)
            {
                platenumber[activeIndex] = item;
                plateNumberButtons[activeIndex].Text = item;
                if (activeIndex < 7 && !this.checkSpesialCharacter())
                {
                    activeIndex++;
                }
                UpdateButtonStates();
            }
        }

        private bool checkSpesialCharacter()
        {
            for (int i = 0; i < this.font.Length; i++)
            {
                if (this.font[i].Equals(platenumber[6]))
                {
                    plateNumberButtons[7].Text = "";
                    plateNumberButtons[7].Enabled = false;
                    return true;
                }
                else
                {
                    plateNumberButtons[7].Enabled = true;
                }
            }
            return false;
        }

        private void ColorButton_Click(int index)
        {
            colorIndex = index;
            // 更新输入框背景颜色
            FlowLayoutPanel plateInputPanel = this.Controls.OfType<FlowLayoutPanel>().FirstOrDefault(p => p.Controls.Contains(plateNumberButtons[0]));
            switch (platecolorlist[colorIndex])
            {
                case "blue":
                    plateInputPanel.BackColor = Color.DeepSkyBlue;
                    break;
                case "yellow":
                    plateInputPanel.BackColor = Color.FromArgb(230, 162, 60);
                    break;
                case "green":
                    plateInputPanel.BackColor = Color.FromArgb(103, 194, 58);
                    break;
                case "yellow-green":
                    plateInputPanel.BackColor = Color.FromArgb(194, 221, 54);
                    break;
                case "white":
                    plateInputPanel.BackColor = Color.LightGray;
                    break;
                case "black":
                    plateInputPanel.BackColor = Color.DimGray;
                    break;
            }
        }

        private void UpdateButtonStates()
        {
            // 更新车牌号按钮状态
            for (int i = 0; i < plateNumberButtons.Count; i++)
            {
                plateNumberButtons[i].BackColor = i == activeIndex ? Color.Gray : SystemColors.Control;
            }

            // 更新省份按钮状态
            foreach (Button button in provinceButtons)
            {
                button.Enabled = activeIndex == 0;
            }

            // 更新数字和字母按钮状态
            int j = 0;
            foreach (Button button in numberButtons)
            {
                if (j < 10) {
                    button.Enabled = activeIndex != 0 && activeIndex != 1;
                }
                else
                {
                    button.Enabled = activeIndex != 0;
                }  
                j++;
            }

            // 更新特殊字符按钮状态
            foreach (Button button in fontButtons)
            {
                button.Enabled = (activeIndex == 6 || activeIndex == 7);
            }
        }
    }
}